package com.dgut.coal.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.dgut.coal.entity.Audit;
import com.dgut.coal.entity.Purchase;
import com.dgut.coal.service.AuditService;
import com.dgut.coal.service.PurchaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: Yang
 * @Description:
 * @Date: Created in 17:58 2019/10/3
 * @Modifued BY:
 */

@Controller
public class PurchaseController {

    @Autowired
    PurchaseService ps;

    @Autowired
    AuditService as;

    @RequestMapping("/procurementForm")
    public String from(){
        return "procurement-form";
    }

    @RequestMapping("/procurementList")
    public String list(Map<String, Object> map,String bill_number,Integer state, @RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Purchase> list = ps.list(bill_number,state);
        PageInfo pageInfo = new PageInfo<>(list,5);
        List keywords = Arrays.asList(bill_number,state);
        map.put("pageInfo",pageInfo);
        map.put("keywords",keywords);

        return "procurement-list";
    }
    /**
     * commit是将订单直接提交，状态待审核
     * @param purchase
     * @return
     */
    @RequestMapping("/commit")
    @ResponseBody
    public int commit(Purchase purchase){
        int row = ps.commit(purchase);
        return row;
    }

    /**
     * commit是将订单保存，状态草稿
     * @param purchase
     * @return
     */
    @RequestMapping("/savePurchase")
    @ResponseBody
    public int savepurchase(Purchase purchase){
        int row = ps.save(purchase);
        return row;
    }

    @RequestMapping("/deletePurchase")
    @ResponseBody
    public int delete(Integer apply_id){

        int row = ps.deletePurchase(apply_id);
        return row;
    }

    @RequestMapping("/viewPurchase")
    public String viewPurchase(Integer apply_id,Map<String, Object> map){
        Purchase purchase = ps.viewPurchase(apply_id);
        List<Audit> list = as.auditList(apply_id);
        map.put("auditList",list);
        map.put("purchase",purchase);
        return "procurement-view";
    }

    @RequestMapping("/editPurchase")
    public String editPurchase(Integer apply_id, Map<String, Object> map){
        Purchase purchase = ps.viewPurchase(apply_id);
        map.put("purchase",purchase);
        return "procurement-edit";
    }

    /**
     * 对草稿或者已驳回状态的订单进行修改，修改完直接提交，状态变成审核中
     * @param purchase
     * @return
     */
    @RequestMapping("/updateCommit")
    @ResponseBody
    public int updateCommit(Purchase purchase){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String edit_date = format.format(new Date());

        int row = ps.updateCommit(purchase,edit_date);
        return row;
    }

    /**
     * 对草稿或者已驳回状态的订单进行修改，修改完直接保存，状态变成草稿
     * @param purchase
     * @return
     */
    @RequestMapping("/updateSave")
    @ResponseBody
    public int updateSave(Purchase purchase){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String edit_date = format.format(new Date());
        int row = ps.updateSave(purchase,edit_date);
        System.out.println(row);
        return row;
    }

    /**
     * 审核列表展示页面 + 查询页面
     * @param map
     * @param pageNum
     * @param apply_id
     * @param bill_number
     * @return
     */
    @RequestMapping("/auditPur")
    public String auditPur(Map<String, Object> map,@RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum, @RequestParam(required = false,defaultValue = "",value = "apply_id") Integer apply_id, @RequestParam(required = false,defaultValue = "",value = "bill_number") String bill_number){
        PageHelper.startPage(pageNum,5);
        List<Purchase> list = ps.auditPur(apply_id,bill_number);
        PageInfo pageInfo = new PageInfo<>(list,5);
        List keywords = Arrays.asList(apply_id,bill_number);
        map.put("keywords",keywords);
        map.put("pageInfo",pageInfo);

        return "procurement-audit-list";
    }

    /**
     * 单条审核记录的详情页面，可在页面中进行审核
     * @param apply_id
     * @param map
     * @return
     */
    @RequestMapping("/auditPurchase")
    public String auditPurchase(Integer apply_id,Map<String, Object> map){
        Purchase purchase = ps.viewPurchase(apply_id);

        List<Audit> list = as.auditList(apply_id);
        map.put("auditList",list);

        map.put("purchase",purchase);
        return "procurement-audit";
    }


    /*@RequestMapping("/excel")
    @ResponseBody
    public int excel(){
        List<Purchase> list = ps.list(null,null);
        javax.swing.filechooser.FileSystemView fsv = javax.swing.filechooser.FileSystemView.getFileSystemView();
        //获取桌面路径
        File fsvHomeDirectory = fsv.getHomeDirectory();
        String fileName = fsvHomeDirectory + "\\采购单"+ System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName, Purchase.class).sheet("采购单").doWrite(list);
        return 1;
    }*/

    @RequestMapping("/deleteMore")
    @ResponseBody
    public int deleteMore(String ids){
        String[] id = ids.split(",");
        int[] ints = new int[id.length];
        for (int i = 0; i < id.length; i++) {
            ints[i] = Integer.parseInt(id[i]);
        }
        int row = ps.deleteMore(ints);
        return row;
    }

}
